package com.open.jack.jk_mpchart

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.util.AttributeSet
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet

/**
 * @TODO
 * @author: clf
 * @since: 2020/12/15
 */
open class JkLineChart : IotLineChart {

    companion object {
        const val TAG = "JkLineChart"
        val highLightColor = Color.parseColor("#5A6BC8")
        val dataLineColor = Color.parseColor("#57AEFF")
        /**
         * 添加一条线
         */
        fun getLineDataSet(
            lineEntries: java.util.ArrayList<Entry>,
            lineColor: Int,
            highLightColor: Int,
            tag: String
        ): LineDataSet {
            return IotLineDataSet(lineEntries, tag).let {
                it.lineWidth = 1f
                it.setDrawCircles(false)
                it.highLightColor = highLightColor
                it.enableDashedHighlightLine(10f, 5f, 0f)
                it.setMode(LineDataSet.Mode.CUBIC_BEZIER)
                it.setCubicIntensity(0.3f)
                it.setDrawValues(false)
                it.color = lineColor
                it
            }
        }
    }

    lateinit var mMarker1: BubbleMarkerView
    lateinit var mMarker2: BubbleMarkerView

    constructor(context: Context) : super(context) {
        initChart(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {

        initChart(attrs)
    }

    protected fun initChart( attrs: AttributeSet?) {
    }

    override fun init() {
        super.init()
        mXAxis.labelRotationAngle = 45f
        showMinMAxHighlight = true
        mMarker1 = BubbleMarkerView(context, R.layout.custom_marker_view1)
        mMarker1.setChartView(this) // For bounds control
        mMarker2 = BubbleMarkerView(context, R.layout.custom_marker_view)
        mMarker2.setChartView(this) // For bounds control
    }


    private val lineChart= JkLineChartUtil()

    public override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    public override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    public override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
    }

    public override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }


    override fun initChart(){
        lineChart.initCharView(this)
    }

    override fun setXAxisLabel(xIndex:Int, label:String) {
        lineChart.setXAxisLabel(xIndex,label)
    }

    override fun setLeftAxisTextColor(color:String) {
       lineChart.setLeftAxisTextColor(color)
    }
    override fun setXAxisTextColor(color:String) {
        lineChart.setXAxisTextColor(color)
    }
    //leftAxis.textColor = Color.BLACK;//Color.parseColor("#5A6BC8")


    fun addLineDataList(type:Int,linesData:List<Float>){
        lineChart.addLineData(type,linesData)
    }

    fun setLineColor(type:Int,color:String) {
        lineChart.setLineColor(type,color)
    }

    override fun updateView(){
        lineChart.updateView()
    }

    override fun drawMarkers(canvas: Canvas?) {
        //super.drawMarkers(canvas)
        // if there is no marker view or drawing marker is disabled
        if (!isDrawMarkersEnabled || !valuesToHighlight()) return

        for (i in mIndicesToHighlight.indices) {
            val highlight = mIndicesToHighlight[i]
            val set: ILineDataSet? = mData.getDataSetByIndex(highlight.dataSetIndex)
            if (set == null) continue
            val e = mData.getEntryForHighlight(mIndicesToHighlight[i])
            val entryIndex = set.getEntryIndex(e)

            // make sure entry not null
            if (e == null || entryIndex > set.entryCount * mAnimator.phaseX) continue
            val pos = getMarkerPosition(highlight)

            // check bounds
            if (!mViewPortHandler.isInBounds(pos[0], pos[1])) continue

            // callbacks to update the content
            if (e.y>=10){
                mMarker2.refreshContent(e, highlight)
                mMarker2.draw(canvas, pos[0], pos[1])
            }else{
                mMarker1.refreshContent(e, highlight)
                mMarker1.draw(canvas, pos[0], pos[1])
            }
            /*  mMarker.refreshContent(e, highlight)
              // draw the marker
              mMarker.draw(canvas, pos[0], pos[1])*/
        }
    }

}